package com.runfast.waimai.web.controller.Driver_controller;

import com.runfast.common.web.entity.Result;
import com.runfast.common.web.entity.ResultCode;
import com.runfast.paotui.dao.model.*;
import com.runfast.paotui.dao.model.RunfastAgentbusiness;
import com.runfast.paotui.dao.model.RunfastCuser;
import com.runfast.paotui.dao.model.RunfastCuseraddress;
import com.runfast.paotui.dao.model.RunfastShopper;
import com.runfast.paotui.service.*;
import com.runfast.waimai.dao.model.*;
import com.runfast.waimai.service.RunfastBusinessService;
import com.runfast.waimai.service.RunfastCreditlevelService;
import com.runfast.waimai.service.RunfastGoodsSellRecordService;
import com.runfast.waimai.service.RunfastTransorderService;
import com.runfast.waimai.util.MsgPushUtils;
import com.runfast.waimai.util.MyUtil;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping(value = "/shopper/*")
public class ShopperOrderFirstController extends DriverBaseController {
    //跑腿定单信息
    @Resource
    OrderService orderService;
    @Resource
    private RunfastGoodsSellRecordService goodsSellRecordService;
    //RunfastGoodsSellRecord// 外卖商品订单
    @Resource
    private RunfastBusinessService businessService;
    //RunfastBusiness商家信息
    @Resource
    private RunfastShopperService shopperService;
    //骑手信息
    @Resource
    private RunfastCuserService cuserService;
    ////用户信息
    @Resource
    RunfastCuseraddressService service;
    @Resource
    private RunfastTransorderService transOrderService;
    @Resource
    private  RunfastCuserService runfastCuserService;
    @Resource
    RunfastAgentbusinessService runfastAgentbusinessService;

    @Resource
    private RunfastCreditlevelService creditLevelService;

    protected static Logger LOG = LoggerFactory.getLogger(MsgPushUtils.class);

    @RequestMapping(value = "businessmsg", method = RequestMethod.POST)//获得商家信息
    public @ResponseBody
    ModelMap businessmsg(
            Integer id) {
        RunfastBusiness runfastBusiness = businessService.findById(id);//通过商家id获取商家信息
        ModelMap model = new ModelMap();
        model.put("runfastBusiness", runfastBusiness);
        return model;
    }

    ////用户信息
    @RequestMapping(value = "cusermsg", method = RequestMethod.POST)//获得商家信息
    public @ResponseBody
    ModelMap cusermsg(
            Integer id) {
        RunfastCuser cuser = cuserService.get(id);//通过商家id获取商家信息
        ModelMap model = new ModelMap();
        model.put("cuser", cuser);
        return model;
    }
    @PostMapping("paotuicount")
    public @ResponseBody  ModelMap paotuicount(String token, @RequestParam(value = "page",required = false,defaultValue = "0")Integer page,
                                             @RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize){
        ModelMap modelMap=new ModelMap();
        Integer page1 = page * pageSize;

        //重新判断
        RunfastShopper loginShopper = super.getLoginShopper(token);
        if(loginShopper!=null && (loginShopper.getWorking()==null || loginShopper.getWorking()==0)){
            modelMap.put("worked",0);//worked 1为上班 其他为休息
            modelMap.put("allrow_1", 0);
            modelMap.put("allrow_2",0);
            modelMap.put("allrow_3", 0);
            modelMap.put("allrow_4",0);
            modelMap.put("allrow_5",0);
            return modelMap;
        }

        modelMap.put("worked",1);


        RunfastAgentbusiness runfastAgentbusiness=runfastAgentbusinessService.findByid(super.getLoginShopper(token).getAgentId());
       Integer count1= orderService.findAgentIdAndStatuscount(super.getLoginShopper(token).getAgentId(), 2, page1, pageSize);
       if(runfastAgentbusiness.getOrderMode()==null || runfastAgentbusiness.getOrderMode()!=2){
           modelMap.put("allrow_1",count1);//新任务
       }else {
           modelMap.put("allrow_1",0);
       }

//        CSTest.Token.setDescription(token);
//        modelMap.put("token",CSTest.Token.getDescription());
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date();

        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.add(Calendar.HOUR,-1);

        Date dt1=rightNow.getTime();
       Integer count2 = orderService.findByShopperidAndStatusAndbookTimecount(super.getLoginShopper(token).getId(), 3,dt1);
        modelMap.put("allrow_2",count2);//待取货
   //     Integer count3 = orderService.findByShopperidAndStatuscount(super.getLoginShopper(token).getId(), 2);
        Integer count3 = orderService.findByShopperidAndStatuscount3(super.getLoginShopper(token).getId(), 5);
        System.out.println("count3的数是"+count3);
        modelMap.put("allrow_3",count3);//待送达
        Integer count4 = orderService.findByShopperidAndStatuscount4(super.getLoginShopper(token).getId());
        modelMap.put("allrow_4",count4); //异常单
    /*    DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date();

        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.add(Calendar.HOUR,+1);

        Date dt1=rightNow.getTime();*/
        Integer count5=orderService.findBookTimecount(super.getLoginShopper(token).getId(),dt1,super.getLoginShopper(token).getAgentId());
        modelMap.put("allrow_5",count5);//预定单
       return modelMap;
    }

    @RequestMapping(value = "paotuiindex", method = RequestMethod.POST)
    public @ResponseBody ModelMap paotuiindex(
            String token, //骑手id
            Integer a ,
            @RequestParam(value = "page",required = false,defaultValue = "0")Integer page,
            @RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize
    ) {
        Integer page1 = page * pageSize;
        ModelMap model = new ModelMap();
        //System.out.println("起码我进来了：11111111111111111111111111111111111111111111111111111111111111111111111");
        RunfastShopper shopper = super.getLoginShopper(token);

        if(shopper!=null && (shopper.getWorking()==null || shopper.getWorking()==0)){
            model.put("worked",0);//worked 1为上班，其他为休息
            model.put("msg","骑手处于收工状态");
            model.put("success",false);
            return model;
        }

        model.put("worked",1);




        if (null != shopper) {
            if (a == 1) {

                RunfastAgentbusiness runfastAgentbusiness=runfastAgentbusinessService.findByid(shopper.getAgentId());
                if((runfastAgentbusiness.getOrderMode()==null || runfastAgentbusiness.getOrderMode()!=2) && shopper.getWorking()==1){
        //        List<Map<String, Object>> orders = orderService.findAgentIdAndStatus(super.getLoginShopper(token).getAgentId(), 0, page1, pageSize);
             List<Order>  orders = orderService.findAgentIdAndStatus1(super.getLoginShopper(token).getAgentId(), 2, page1, pageSize);

             System.out.println("总共有多少条数据:=============="+orders.size());

             //2为用户已付款的 ,骑手为空的
//                Order order=orderService.findbyid(id);
                DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Calendar Cal=Calendar.getInstance();
//                Cal.setTime(order.getCreateTime());
//
//                Cal.add(java.util.Calendar.MINUTE,+order.getDeliveryDuration());
//                String time=dateFormat.format(Cal.getTime());
                System.out.println(orders);

                if (orders.size()==0) {
                    model.put("success", false);
                    model.put("msg", "没有新任务");

                } else {
             //       model.put("token",CSTest.Token.getDescription());
                    for (Order s:orders){

                       System.out.println(s+"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
                  //      System.out.println(from+"bAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
                        RunfastCuseraddress  runfastCuser= service.findById(s.getToId()) ;
                        RunfastCuseraddress  runfastCuser1= service.findById(s.getFromId()) ;
                        RunfastCuser s1=runfastCuserService.get(runfastCuser.getCid());
                        System.out.println(runfastCuser.getGender());

               //         RunfastCuser from=runfastCuserService.get(runfastCuser1.getCid());

                                if(null!=runfastCuser1){
                                    s.setFromAddress(runfastCuser1.getUserAddress()+runfastCuser1.getAddress());
                                    s.setFromPhone(runfastCuser1.getPhone());
                                    s.setFromName(runfastCuser1.getName());
                                    s.setFromLng(Double.valueOf(runfastCuser1.getLatitude()));
                                    s.setFromLat(Double.valueOf(runfastCuser1.getLongitude()));
                                    RunfastCuser from=runfastCuserService.get(runfastCuser1.getCid());
                                   if(from!=null) {
                                        s.setFromgender(runfastCuser1.getGender());
                                    }

                                }else {
                                    System.out.println("那就不传");
                                }
                        Cal.setTime(s.getCreateTime());

                        Cal.add(Calendar.MINUTE,+s.getDeliveryDuration());
                        String time=dateFormat.format(Cal.getTime());
                             s.setToName(runfastCuser.getName());
                                s.setToMobile(runfastCuser.getPhone());
                                s.setToAddress(runfastCuser.getUserAddress()+runfastCuser.getAddress());

                                 //s.setOverTime(time);//普通单预计送达时间

                        if(s.getPickTime()!=null){
                            //立即取件|剩余时间
                            s.setOverTime(dateFormat.format(s.getPickTime()));
                            long b = s.getPickTime().getTime() - new Date().getTime();
                            long min = b / (60 * 1000);
                            s.setReturnTime(min);

                        }else{
                            //预送达时间|剩余时间
                            if(s.getBookTime()!=null){
                                s.setOverTime(dateFormat.format(s.getBookTime()));
                                long b2 = s.getBookTime().getTime() - new Date().getTime();
                                long min2 = b2 / (60 * 1000);
                                s.setReturnTime(min2);
                            }
                        }

                        if(s1!=null){
                            s.setGender(runfastCuser.getGender());
                        }
                        Date disTime=Cal.getTime();
                        Date createTime=s.getCreateTime();
                        Date newdate=new Date();

                       /* if(disTime!=null&&createTime!=null){
                            long b=disTime.getTime()-newdate.getTime();
                            long min=b/(60*1000);
                            //        model.put("remainMin", min);
                            //System.out.println("预计送达");
                            s.setReturnTime(min);//普通单剩余时间
                        }*//*else{
                            long b2=s.getBookTime().getTime()-newdate.getTime();
                            long min2=b2/(60*1000);
                            //（立即送达）剩余时间和预计送达时间
                            //System.out.println("立即送达");
                            s.setReturnTime(min2);
                            Calendar Cal2=Calendar.getInstance();
                            Cal2.setTime(s.getBookTime());
                            String format = dateFormat.format(Cal2.getTime());
                            s.setOverTime(format);
                        }*/

                     //   model.put("overTime",time);
            }
                    System.out.println(orders+"***********************************************************");

                    model.put("success", true);
                    model.put("order", orders);
                }}else {
                    model.put("success", false);
                    model.put("msg","骑手休息了" );
                }
            }
            if (a == 2) {
                DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date=new Date();

                Calendar rightNow = Calendar.getInstance();
                rightNow.setTime(date);
                rightNow.add(Calendar.HOUR,-1);

                Date dt1=rightNow.getTime();
                List<Order> orders = orderService.findByShopperidAndStatusAndbookTime(super.getLoginShopper(token).getId(), 3,dt1);
           //     List<Order> orders = orderService.findByShopperidAndStatus(super.getLoginShopper(token).getId(), 3,0,1000);
                System.out.println(orders.size()+"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
                //a 2 已付款的，shopper接单的
                DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Calendar Cal=Calendar.getInstance();

               // System.out.println("妖怪去哪里："+orders);

                if (orders.size()==0) {
                    model.put("msg", "没有商品要取");
                    model.put("success", false);
                }else {
                    for (Order s:orders){



                        RunfastCuseraddress  runfastCuser= service.findById(s.getToId()) ;
                        RunfastCuseraddress  runfastCuser1= service.findById(s.getFromId()) ;
                        RunfastCuser s1=runfastCuserService.get(runfastCuser.getCid());

                        if(null!=runfastCuser1){
                            s.setFromAddress(runfastCuser1.getUserAddress()+runfastCuser1.getAddress());
                            s.setFromPhone(runfastCuser1.getPhone());
                            s.setFromName(runfastCuser1.getName());
                            s.setFromLng(Double.valueOf(runfastCuser1.getLatitude()));
                            s.setFromLat(Double.valueOf(runfastCuser1.getLongitude()));
                            RunfastCuser from=runfastCuserService.get(runfastCuser1.getCid());
                            if(from!=null) {
                                s.setFromgender(runfastCuser1.getGender());

                            }
                        }else {
                            System.out.println("那就不传");
                        }
                        Cal.setTime(s.getCreateTime());

                        Cal.add(Calendar.MINUTE,+s.getDeliveryDuration());
                        String time=dateFormat.format(Cal.getTime());
                        s.setToName(runfastCuser.getName());
                        s.setToMobile(runfastCuser.getPhone());
                        s.setToAddress(runfastCuser.getUserAddress()+runfastCuser.getAddress());

                        //现在的时间计算
                        if(s.getPickTime()!=null){
                            //立即取件|剩余时间
                            s.setOverTime(dateFormat.format(s.getPickTime()));
                            long b = s.getPickTime().getTime() - new Date().getTime();
                            long min = b / (60 * 1000);
                            s.setReturnTime(min);

                        }else{
                            //预送达时间|剩余时间
                            if(s.getBookTime()!=null){
                                s.setOverTime(dateFormat.format(s.getBookTime()));
                                long b2 = s.getBookTime().getTime() - new Date().getTime();
                                long min2 = b2 / (60 * 1000);
                                s.setReturnTime(min2);
                            }
                        }


                        if(s1!=null){ s.setGender(runfastCuser.getGender());}
                        //之前的时间计算
                       /* s.setOverTime(time);
                        if(s1!=null){ s.setGender(runfastCuser.getGender());}
                        Date disTime=Cal.getTime();
                        Date createTime=s.getCreateTime();
                        Date newdate=new Date();

                        if(disTime!=null&&createTime!=null){
                            long b=disTime.getTime()-newdate.getTime();
                            long min=b/(60*1000);
                            //        model.put("remainMin", min);
                            s.setReturnTime(min);
                        }*//*else{
                            long b2=s.getBookTime().getTime()-newdate.getTime();
                            long min2=b2/(60*1000);
                            //（立即送达）剩余时间和预计送达时间
                            //System.out.println("立即送达");
                            s.setReturnTime(min2);
                            Calendar Cal2=Calendar.getInstance();
                            Cal2.setTime(s.getBookTime());
                            String format2 = dateFormat.format(Cal2.getTime());
                            s.setOverTime(format2);
                        }*/

                 //       model.put("overTime",time);
                    }

                   // System.out.println("我在这里："+orders);
                    model.put("order", orders);
                    model.put("success", true);
                    model.put("pagtui", "跑腿");
                }

            }
            if (a == 3) {

                List<Order> orders = orderService.findByShopperidAndStatus(super.getLoginShopper(token).getId(), 5,0,1000);//a3为待送达
                DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Calendar Cal=Calendar.getInstance();

                if (orders.size()==0) {
                    model.put("msg", "没有要送的跑腿定单");
                    model.put("success", false);
                }else {
                    List<Order> list=new ArrayList<Order>();
                    List<Order>list1=new ArrayList<Order>();
                    for (Order s:orders){


                        RunfastCuseraddress  runfastCuser= service.findById(s.getToId()) ;
                        RunfastCuseraddress  runfastCuser1= service.findById(s.getFromId()) ;
                        RunfastCuser s1=runfastCuserService.get(runfastCuser.getCid());
                        if(null!=runfastCuser1){
                            s.setFromAddress(runfastCuser1.getUserAddress()+runfastCuser1.getAddress());
                            s.setFromPhone(runfastCuser1.getPhone());
                            s.setFromName(runfastCuser1.getName());
                            s.setFromLng(Double.valueOf(runfastCuser1.getLatitude()));
                            s.setFromLat(Double.valueOf(runfastCuser1.getLongitude()));
                            RunfastCuser from=runfastCuserService.get(runfastCuser1.getCid());
                            if(from!=null) {
                                s.setFromgender(runfastCuser1.getGender());
                            }
                        }else {
                            System.out.println("那就不传");
                        }
                        Cal.setTime(s.getCreateTime());

                        Cal.add(Calendar.MINUTE,+s.getDeliveryDuration());
                        String time=dateFormat.format(Cal.getTime());
                        s.setToName(runfastCuser.getName());
                        s.setToMobile(runfastCuser.getPhone());
                        s.setToAddress(runfastCuser.getUserAddress()+runfastCuser.getAddress());

                        //现在的时间计算
                        if(s.getPickTime()!=null){
                            //立即取件|剩余时间
                            s.setOverTime(dateFormat.format(s.getPickTime()));
                            long b = s.getPickTime().getTime() - new Date().getTime();
                            long min = b / (60 * 1000);
                            s.setReturnTime(min);

                        }else{
                            //预送达时间|剩余时间
                            if(s.getBookTime()!=null){
                                s.setOverTime(dateFormat.format(s.getBookTime()));
                                long b2 = s.getBookTime().getTime() - new Date().getTime();
                                long min2 = b2 / (60 * 1000);
                                s.setReturnTime(min2);
                            }
                        }

                        if(s1!=null){ s.setGender(runfastCuser.getGender());}
                       /* s.setOverTime(time);
                        Date disTime=Cal.getTime();
                        Date createTime=s.getCreateTime();
                        Date newdate=new Date();

                        if(disTime!=null&&createTime!=null){
                            long b=disTime.getTime()-newdate.getTime();
                            long min=b/(60*1000);
                            //        model.put("remainMin", min);
                            s.setReturnTime(min);
                        }*//*else{
                            long b2=s.getBookTime().getTime()-newdate.getTime();
                            long min2=b2/(60*1000);
                            //（立即送达）剩余时间和预计送达时间
                            //System.out.println("立即送达");
                            s.setReturnTime(min2);
                            Calendar Cal2=Calendar.getInstance();
                            Cal2.setTime(s.getBookTime());
                            String format = dateFormat.format(Cal2.getTime());
                            s.setOverTime(format);
                        }*/


                     //   model.put("overTime",time);
                        list.add(s);
                    }
                    if(list == null){//当传入过来的list集合为null时，先进行实例化
                        list = new ArrayList<Order>();
                    }
                    Integer pageNo=page+1;
                    if((Object)pageNo == null){//当传入过来的pageNo为null时，先进行赋值操作
                        pageNo = 1;
                    }
                    if((Object)pageSize == null){//当传入过来的dataSize为null时，先进行赋值操作
                        pageSize = 1;
                    }
                    if(pageNo <= 0){
                        pageNo = 1;
                    }
                    //记录一下数据一共有多少条
                    int totalitems = list.size();
                    //实例化一个接受分页处理之后的数据
                    //        List<Order> afterList = new ArrayList<Order>();
                    for
                            ( int i = (pageNo-1)*pageSize;
                              i < (((pageNo -1)*pageSize) + pageSize >
                                      totalitems ? totalitems:((pageNo -1)*pageSize) +pageSize);
                              i++) {
//然后将数据存入afterList中

                        list1.add(list.get(i));
                    }


                    if(list1.size()>0){
                 //       System.out.println("aaaaaa"+list);
                        model.put("order",list1);
              //          model.put("count",count);
                        model.put("success",true);
                    }else {
                        model.put("success",false);
                        model.put("msg","没有数据统计了");

                    }
                 /*   model.put("order", orders);
                    model.put("success", true);
                    model.put("pagtui", "跑腿");*/
                }


            }
            if (a == 4) {
                if (shopper != null) {
               //     List<Order> orders = orderService.findByShopperidAndStatus(super.getLoginShopper(token).getId(), 9,0,1000);//跑腿目前没有异常单
                    List<Order> orders = orderService.findShopperAndStatusErro(super.getLoginShopper(token).getId());
                    if (orders.size() ==0 ) {
                        model.put("msg", "没有异常单");

                        model.put("success", false);
                    } else {
                        for (Order s:orders){


                            RunfastCuseraddress  runfastCuser= service.findById(s.getToId()) ;
                            RunfastCuseraddress  runfastCuser1= service.findById(s.getFromId()) ;

                            if(null!=runfastCuser1){
                                s.setFromAddress(runfastCuser1.getUserAddress()+runfastCuser1.getAddress());
                                s.setFromPhone(runfastCuser1.getPhone());
                                s.setFromName(runfastCuser1.getName());
                                s.setFromLng(Double.valueOf(runfastCuser1.getLatitude()));
                                s.setFromLat(Double.valueOf(runfastCuser1.getLongitude()));
                                RunfastCuser from=runfastCuserService.get(runfastCuser1.getCid());
                                if(from!=null) {
                                    s.setFromgender(runfastCuser1.getGender());
                                }
                            }else {
                                System.out.println("那就不传");
                            }

                            s.setToName(runfastCuser.getName());
                            s.setToMobile(runfastCuser.getPhone());
                            s.setToAddress(runfastCuser.getUserAddress()+runfastCuser.getAddress());
                            RunfastCuser s1=runfastCuserService.get(runfastCuser.getCid());
                            if(s1!=null){ s.setGender(runfastCuser.getGender());}

                        }
                        model.put("order", orders);
                        model.put("success", true);
                        model.put("pagtui", "跑腿");
                    }
                }
            }

        }  return model;
    }

        //显示外卖定单 1为新任务 2 已取货 3为待送达 4 为异常单
        @RequestMapping(value = "index", method = RequestMethod.POST)
        public @ResponseBody ModelMap index ( String token,
                Integer a,
                @RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
                @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize)
        {
            //a1为新任务 a2 待取货 a3为待送达 a4 为异常单



            Integer page1 = page * pageSize;
            //System.out.println("变了么："+page1);
            ModelMap model = new ModelMap();
            //System.out.println(token+"看看");
            RunfastShopper shopper = super.getLoginShopper(token);
           // System.out.println(shopper.getId());


            if( shopper!=null && ( shopper.getWorking()==null ||  shopper.getWorking()==0)){
                model.put("worked",0);//worked 1为上班，其他为休息
                model.put("msg","骑手处于收工状态");
                model.put("success",false);
                return model;
            }

            model.put("worked",1);

           /* RunfastAgentbusiness Agentbusiness=runfastAgentbusinessService.findByid(shopper.getAgentId());
            if(Agentbusiness.getOrderMode()==null){
               model.put("errormsg","订单模式未设置成功，请联系代理商重新设置");
               model.put("success",false);
               return model;
            }*/


            System.out.println("=========================="+shopper);
            if (a == 1) {
                RunfastAgentbusiness runfastAgentbusiness=runfastAgentbusinessService.findByid(shopper.getAgentId());

                /*System.out.println("==="+shopper);
                System.out.println("==="+shopper.getWorking());
                System.out.println("==="+runfastAgentbusiness.getOrderMode());
                System.out.println("代理商："+shopper.getAgentId());*/

                if (shopper != null && shopper.getWorking()==1 && (runfastAgentbusiness.getOrderMode()==null || runfastAgentbusiness.getOrderMode()!=2)) {
                    //getOrderMode骑手接单模式：1抢单，2派单
                    //配送员信息不能为空

                    //通过商家id 用户id 骑手id  查询商家信息
                    //通过商家id 用户id 骑手id 查询买家信息
                    //通过外卖商品订单商家 骑手id ,定单号，定单状态  查询为新任务
                    //     List<RunfastGoodsSellRecord> goodsSellRecord= goodsSellRecordService.findByShopperIdAndStruts(super.getLoginShopper(token).getId(),1);// //a1为新任务
                    //System.out.println(page1+"=="+pageSize);
                    List<RunfastGoodsSellRecord> goodsSellRecord = goodsSellRecordService.findstatusnewmsg( page1, pageSize, shopper.getAgentId());
                    //System.out.println("總共有："+goodsSellRecord.size());
                    //System.out.println(goodsSellRecord);
                    List<RunfastTransorder> runfastTransorders=transOrderService.findAndresshopperid(page1,pageSize,shopper.getId());
                    List<RunfastGoodsSellRecord>list=new ArrayList<RunfastGoodsSellRecord>();
                    Collections.reverse(list);
                    Date newdate=new Date();
                        for(RunfastGoodsSellRecord s:goodsSellRecord){

                            //目测DaojiTime这个字段没有意义
                            if(s.getBookTime()!=null){
                                Long aa=s.getBookTime().getTime()-new Date().getTime();
                                Long min1=aa/60/1000;
                                s.setDaojiTime(min1);
                                //System.out.println("预送达时间："+s.getBookTime());
                                //System.out.println("预计送达时间2："+s.getDistime());
                            }


                            if(s.getBooked()!=null && s.getBooked()==1 && s.getBookTime()!=null){
                                s.setDistime(s.getBookTime());//立即送达时间
                                long backTime = s.getBookTime().getTime() - newdate.getTime();
                                long min=backTime/(60*1000);
                                s.setReturnTime(min);//剩余时间
                            }else{
                                Date disTime=s.getDistime();
                                //Date createTime=s.getCreatetime();
                                if(disTime!=null){
                                    long b=disTime.getTime()-newdate.getTime();
                                    long min=b/(60*1000);
                                    //        model.put("remainMin", min);
                                    s.setReturnTime(min);//预计送达剩余时间
                                }
                            }

                           list.add(s);
                            //System.out.println(s.getUserAddressGender()+"有没有数据");

                        }
                    System.out.println("看看的************"+goodsSellRecord.size());
                    //订单状态编码-3:商家拒单-1：订单取消  0：客户下单，1：客户已付款  2：商家接单  3：骑手接单   4：商品打包 ，5：商品配送 6：商品送达，7:确认收货 ，8：订单完成 9.异常单
                    if (goodsSellRecord.size()==0) {
                        //model.put("msg", "没有外卖新任务");
                        model.put("success", false);
                    }
                    //System.out.println(list+"检查&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
                    model.put("transOrder",runfastTransorders);


                    model.put("list", list);
                    model.put("success", true);
                    model.put("pagtui", "外卖");
                }else {
                    model.put("success",false);
                    model.put("msg","休息时间不能抢单或者开启了派单模式");
                }
            }
            if (a == 2) {
                    //显示待处货的
                System.out.println("进来了没************************************");
                if (shopper != null) {//配送员信息不能为空
                  //     List<RunfastGoodsSellRecord> goodsSellRecord= goodsSellRecordService.findByShopperIdAndStruts(super.getLoginShopper(token).getId(),3);//a2 待取货
          //          List<RunfastGoodsSellRecord> goodsSellRecord1= goodsSellRecordService.findByShopperIdAndStruts(super.getLoginShopper(token).getId(),4);
                 //   List<RunfastGoodsSellRecord> goodsSellRecord = goodsSellRecordService.findstatus(2, page1, pageSize, shopper.getAgentId());


                    DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date date=new Date();

                    Calendar rightNow = Calendar.getInstance();
                    rightNow.setTime(date);
                    rightNow.add(Calendar.HOUR,+1);

                    Date dt1=rightNow.getTime();
                    List<RunfastGoodsSellRecord> goodsSellRecord = goodsSellRecordService.findstatus(null, page1, pageSize, shopper.getAgentId(),shopper.getId(),dt1);
                    List<RunfastGoodsSellRecord>list=new ArrayList<RunfastGoodsSellRecord>();
                   SimpleDateFormat dateFormat=new SimpleDateFormat("yyyyMMddHHssmm");

                    Date newdate=new Date();

                    for (RunfastGoodsSellRecord s:goodsSellRecord){
                            if(s.getBookTime()!=null){
                                Long aa=s.getBookTime().getTime()-date.getTime();
                                System.out.println("能过去么：");
                                long min1=aa/(60*1000);
                                s.setDaojiTime(min1);
                            }


                        //System.out.println(min1+"倒计时**********************");
//                            RunfastCuser cuser = runfastCuserService.get(s.getUserid());
//                            s.setGender(cuser.getGender());

                        //Date disTime=s.getDistime();
                        //Date createTime=s.getCreatetime();


                        /*if(disTime!=null&&createTime!=null){
                            long b=disTime.getTime()-newdate.getTime();
                            long min=b/(60*1000);
                    //        model.put("remainMin", min);
                            s.setReturnTime(min);
                        }*/

                        if(s.getBooked()!=null && s.getBooked()==1 && s.getBookTime()!=null){
                            s.setDistime(s.getBookTime());//立即送达时间
                            long backTime = s.getBookTime().getTime() - newdate.getTime();
                            long min=backTime/(60*1000);
                            s.setReturnTime(min);//剩余时间
                        }else{
                            Date disTime=s.getDistime();
                            Date createTime=s.getCreatetime();
                            if(disTime!=null){
                                long b=disTime.getTime()-newdate.getTime();
                                long min=b/(60*1000);
                                //        model.put("remainMin", min);
                                s.setReturnTime(min);//预计送达剩余时间
                            }
                        }


                        list.add(s);

                    }

                    if ((goodsSellRecord.size()) ==0) {
                        model.put("msg", "没有商品要取");
                        model.put("success", false);
                    }else {
                    model.put("list",list);

                    model.put("success", true);
                    model.put("pagtui", "外卖");}

                }
            }
            if (a == 3) {

                if (shopper != null) {//配送员信息不能为空
                    //    List<RunfastGoodsSellRecord> goodsSellRecord= goodsSellRecordService.findByShopperIdAndStruts(super.getLoginShopper(token).getId(),5);//a3为待送达
                    List<RunfastGoodsSellRecord> goodsSellRecord = goodsSellRecordService.findListPageBean(pageSize, page1, 5, super.getLoginShopper(token).getId(), shopper.getAgentId(), null);

                    //System.out.println("有没有值："+goodsSellRecord.size()+"=========="+super.getLoginShopper(token).getId()+"-----"+shopper.getAgentId());

                    List<RunfastGoodsSellRecord>list=new ArrayList<RunfastGoodsSellRecord>();
                    List<RunfastGoodsSellRecord>list1=new ArrayList<RunfastGoodsSellRecord>();
                    Date newdate=new Date();
                        for (RunfastGoodsSellRecord s:goodsSellRecord){

                            if(s.getBookTime()!=null){
                                Long aa=s.getBookTime().getTime()-newdate.getTime();
                                long min1=aa/(60*1000);
                                s.setDaojiTime(min1);
                            }

                           /* if(disTime!=null&&createTime!=null){
                                long b=disTime.getTime()-newdate.getTime();
                                long min=b/(60*1000);
                          //      model.put("remainMin", min);
                                s.setReturnTime(min);
                            }*/
                            if(s.getBooked()!=null && s.getBooked()==1 && s.getBookTime()!=null){
                                s.setDistime(s.getBookTime());//立即送达时间
                                long backTime = s.getBookTime().getTime() - newdate.getTime();
                                long min=backTime/(60*1000);
                                s.setReturnTime(min);//剩余时间
                            }else{
                                Date disTime=s.getDistime();
                                Date createTime=s.getCreatetime();
                                if(disTime!=null){
                                    long b=disTime.getTime()-newdate.getTime();
                                    long min=b/(60*1000);
                                    //        model.put("remainMin", min);
                                    s.setReturnTime(min);//预计送达剩余时间
                                }
                            }



                            list.add(s);
                        }
                    if (goodsSellRecord.size()==0) {
                        model.put("msg", "没有要送的单");
                        model.put("success", false);
                    }
                    if(list == null){//当传入过来的list集合为null时，先进行实例化
                        list = new ArrayList<RunfastGoodsSellRecord>();
                    }
                    Integer pageNo=page+1;
                    if((Object)pageNo == null){//当传入过来的pageNo为null时，先进行赋值操作
                        pageNo = 1;
                    }
                    if((Object)pageSize == null){//当传入过来的dataSize为null时，先进行赋值操作
                        pageSize = 1;
                    }
                    if(pageNo <= 0){
                        pageNo = 1;
                    }
                    //记录一下数据一共有多少条
                    int totalitems = list.size();
                    //实例化一个接受分页处理之后的数据
                    //        List<Order> afterList = new ArrayList<Order>();
                    for
                            ( int i = (pageNo-1)*pageSize;
                              i < (((pageNo -1)*pageSize) + pageSize >
                                      totalitems ? totalitems:((pageNo -1)*pageSize) +pageSize);
                              i++) {
                        //然后将数据存入afterList中

                        list1.add(list.get(i));
                    }


                    if(list.size()>0){
                        System.out.println("aaaaaa"+list1);
                        model.put("list", list1);
                        model.put("success", true);
                        model.put("pagtui", "外卖");
                    }else {
                        model.put("success",false);
                        model.put("msg","没有数据统计了");

                    }

                }
            }
            if (a == 4) {


                if (shopper != null) {
                    RunfastGoodsSellRecordExample goodsSellRecordExample=new RunfastGoodsSellRecordExample();
                    goodsSellRecordExample.createCriteria().andShopperidEqualTo(super.getLoginShopper(token).getId()).andStatusBetween(1,8).andIscancelEqualTo(1).andShopperidIsNotNull();

                    //List<RunfastGoodsSellRecord> goodsSellRecord = goodsSellRecordService.findByShopperIdAndStrutsAndagree2(super.getLoginShopper(token).getId(), 9);//agree<9 就是除了跑腿外的异常

                    //刷新


                    List<RunfastGoodsSellRecord> goodsSellRecord = goodsSellRecordService.selectByExampleWithPageable(goodsSellRecordExample, PageRequest.of(page, pageSize));

                    List<RunfastGoodsSellRecord>list=new ArrayList<RunfastGoodsSellRecord>();
                    for (RunfastGoodsSellRecord s:goodsSellRecord){
                        Date disTime=s.getDistime();
                        Date createTime=s.getCreatetime();
                        Date newdate=new Date();

                        if(disTime!=null&&createTime!=null){
                            long b=disTime.getTime()-newdate.getTime();
                            long min=b/(60*1000);
                            //      model.put("remainMin", min);
                            s.setReturnTime(min);
                        }


                        list.add(s);
                    }
                    if (goodsSellRecord.size()==0) {
                        model.put("msg", "没有异常单");
                        model.put("success", false);
                    } else {
                        model.put("list", list);
                        model.put("success", true);
                        model.put("pagtui", "外卖");
                    }

                }
            }

            return model;
        }
        /*跑腿定单详情*/
        @PostMapping("paotuiordermsg")
    public @ResponseBody ModelMap paotuiordermsg(Integer id,String token,String Longitude,String Latitude){//添加了骑手token
        ModelMap model=new ModelMap();
         Order order=orderService.findbyid(id);
         RunfastShopper loginShopper = super.getLoginShopper(token);//当前骑手查看详情


           /* RunfastCreditlevel runfastCreditlevel = creditLevelService.selectByPrimaryKey(loginShopper.getCreditLevelid());
            //查看骑手信誉等级问题
            if(loginShopper.getCreditLevelid()==null || runfastCreditlevel==null){
                model.put("level",false);//false当前骑手绑定等级信誉不存在，或者绑定错误
                model.put("success", false);
                model.put("errormsg", "当前信誉值不存在，请尽快联系代理商");
                return model;
            }*/

            if(order!=null){
                RunfastCuseraddress  runfastCuser= service.findById(order.getToId()) ;
                //RunfastCuseraddress  runfastCuser1= service.findById(order.getFromId());


                RunfastCuser s1=runfastCuserService.get(runfastCuser.getCid());
                if(s1!=null){
                    order.setGender(runfastCuser.getGender());
                }

                order.setToMobile(runfastCuser.getPhone());
                order.setToName(runfastCuser.getName());
                order.setToAddress(runfastCuser.getUserAddress()+runfastCuser.getAddress());


               /* if(null!=runfastCuser1){
                    //(11-08)
                    *//*order.setFromAddress(runfastCuser1.getUserAddress()+runfastCuser1.getAddress());
                    order.setFromPhone(runfastCuser1.getPhone());
                    order.setFromName(runfastCuser1.getName());
                    order.setFromLng(Double.valueOf(runfastCuser1.getLongitude()));
                    order.setFromLat(Double.valueOf(runfastCuser1.getLatitude()));
                    RunfastCuser from=runfastCuserService.get(runfastCuser1.getCid());
                    if(s1!=null){order.setFromgender(runfastCuser1.getGender());}*//*
                    //Double shopdis = MyUtil.getDistance(Double.parseDouble(shopper.getLongitude()), Double.parseDouble(shopper.getLatitude()), order.getFromLng(), order.getFromLat());
                }else {
                    System.out.println("那就不传");
                }*/
                DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Calendar Cal=Calendar.getInstance();
                Cal.setTime(order.getCreateTime()); //设置当前系统时间

                Cal.add(Calendar.MINUTE,+order.getDeliveryDuration());
                String time=dateFormat.format(Cal.getTime());


                if(order.getPickTime()!=null){
                    //立即取件|剩余时间
                    order.setOverTime(dateFormat.format(order.getPickTime()));
                    long b = order.getPickTime().getTime() - new Date().getTime();
                    long min = b / (60 * 1000);
                    order.setReturnTime(min);

                }else{
                    //预送达时间|剩余时间
                    if(order.getBookTime()!=null){
                        order.setOverTime(dateFormat.format(order.getBookTime()));
                        long b2 = order.getBookTime().getTime() - new Date().getTime();
                        long min2 = b2 / (60 * 1000);
                        order.setReturnTime(min2);
                    }
                }
               // order.setOverTime(time);
                Date disTime=Cal.getTime();
                Date createTime=order.getCreateTime();
                Date newdate=new Date();


                System.out.println("骑手id:============="+order.getDriverId());
                //坐标
                if(null!=loginShopper){
                    Double shopdis=0.0;
                    //iso实时获取骑手坐标
                    if(Latitude!=null && Longitude!=null && Latitude!="" && Longitude!=""){
                        shopdis = MyUtil.getDistance( Double.parseDouble(Latitude),Double.parseDouble(Longitude), order.getFromLat(),order.getFromLng());
                    }
                    //安卓实时自带骑手坐标
                    String latitude = loginShopper.getLatitude();
                    String longitude = loginShopper.getLongitude();
                   if(latitude!=null && longitude!=null && latitude!="" && longitude!=""){
                        shopdis = MyUtil.getDistance( Double.parseDouble(latitude),Double.parseDouble(longitude), order.getFromLat(),order.getFromLng());
                    }

                    //System.out.println("沒有進來麼"+shopdis);
                order.setDistance(shopdis.intValue());
                }


                //跑腿支付佣金(骑手的佣金部分)(待修改================================================================)
                // order.getDriverIncome();
               /* Double aDouble = Double.valueOf(order.getDeliveryCost().toString());//配送费
                System.out.println("配送费："+order.getDeliveryCost());
                double aDouble1=0.0;//小费
                if(order.getTip()!=null){
                    aDouble1 = Double.valueOf(order.getTip().toString());//小费
                }
                double v = aDouble * 0.8/100+aDouble1;
                Double aDouble2 = Double.valueOf(v);
                int driver_income = aDouble2.intValue();
                System.out.println("骑手的收入："+driver_income);*/
                //System.out.println("代理商：================="+loginShopper);
                com.runfast.paotui.dao.model.RunfastAgentbusiness runfastAgentbusiness=runfastAgentbusinessService.findByid(loginShopper.getAgentId());


                if(order.getTip()!=null){
                    //配送費+打賞
                    order.setDriverIncome((int) (order.getDeliveryCost()*(1-runfastAgentbusiness.getPaotuiCoefficient().doubleValue()/100-loginShopper.getCoefficient().doubleValue()/100))+order.getTip());//骑手收入
                }else{
                    //配送費
                    order.setDriverIncome((int) (order.getDeliveryCost()*(1-runfastAgentbusiness.getPaotuiCoefficient().doubleValue()/100-loginShopper.getCoefficient().doubleValue()/100)));//骑手收入
                }



              /* System.out.println("剩下的百分比：==============="+(1-runfastAgentbusiness.getPaotuiCoefficient().doubleValue()/100-loginShopper.getCoefficient().doubleValue()/100));
                System.out.println("剩下的百分比2：==============="+(1-runfastAgentbusiness.getPaotuiCoefficient().intValue()/100-loginShopper.getCoefficient().intValue()/100));

                System.out.println("骑手收入："+order.getAmountPayable()*(1-runfastAgentbusiness.getPaotuiCoefficient().intValue()/100-loginShopper.getCoefficient().intValue()/100));
                System.out.println("骑手收入3："+order.getAmountPayable()*(1-runfastAgentbusiness.getPaotuiCoefficient().doubleValue()/100-loginShopper.getCoefficient().doubleValue()/100));*/

                //待修改============================================================================================
//                for (Order s:orders){
//
//                    RunfastCuseraddress  runfastCuser= service.findById(s.getToId()) ;
//                    RunfastCuseraddress  runfastCuser1= service.findById(s.getFromId()) ;
//                    if(null!=runfastCuser1){
//                        s.setFromAddress(runfastCuser1.getUserAddress()+runfastCuser1.getAddress());
//                        s.setFromLng(Double.valueOf(runfastCuser1.getLatitude()));
//                        s.setFromLat(Double.valueOf(runfastCuser1.getLongitude()));
//                    }else {
//                        System.out.println("那就不传");
//                    }
//
//                    s.setToName(runfastCuser.getName());
//                    s.setToMobile(runfastCuser.getPhone());
//                    s.setToAddress(runfastCuser.getUserAddress()+runfastCuser.getAddress());
//                }

         if(order.getType()==Order.Type.DAIGOU){
             //旧近购买

             if(order.getFromType()==Order.FromType.NEARBY){

         //        model.put("cuser",runfastCuser);
        //        model.put("overTime",time);
                model.put("order",order);
                model.put("msg","就近购买");
                model.put("success",true);
             }
             //指定地址
             if(order.getFromType()==Order.FromType.SPECIFIED){
         //        model.put("cuser",runfastCuser);
       //          model.put("overTime",time);
                 model.put("order",order);
                 model.put("msg","指定购买");
                 model.put("success",true);
             }
         }
            //取送件
            if(order.getType()==Order.Type.QUSONGJIAN){

       //         model.put("cuser",runfastCuser);
     //           model.put("overTime",time);
                model.put("order",order);
                model.put("msg","取送件");
                model.put("success",true);
            }
            }
         return model;
        }
    }